home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Utilities / Programming / EnterAct 3.5 / Drag_on Modules / hAWK programs / $LongestLines < prev    next >
Encoding:
Text File  |  1993-04-09  |  3.6 KB  |  132 lines  |  [TEXT/????]

  1. #$LongestLines: print list of longest lines in a document
  2. #Variables:
  3. #    how_many=number of long lines to track
  4. #                (default 8 longest)
  5. #    spaces_in_tab=the number of spaces in a tab
  6. #                (default 4 spaces)
  7. #    acceptable_length=length below which line is
  8. #                ignored here (default 40 characters)
  9.  
  10. #Use with any input option, sends results to stdout.
  11.  
  12. #A tab counts for 1 to spaces_in_tab spaces, never 0, and always dependent
  13. #on position: if position on line counted from 0 up, then
  14. #on hitting a tab, go to next tab “stop” with
  15. #    stop = position/spaces_in_tab + 1
  16. #    position = stop * spaces_in_tab
  17. #    or position = (position/spaces_in_tab + 1) * spaces_in_tab
  18.  
  19. # User’s Manual references:
  20. # «hAWK User’s Manual» «F   Running hAWK programs»
  21. # «hAWK User’s Manual» «L  5   Regular expressions»
  22. # «hAWK User’s Manual» «M  5   Built-in string and file functions»
  23. # «hAWK User’s Manual» «K  4   Built-in variables»
  24. # «hAWK User’s Manual» «K  8   Arrays»
  25. # «hAWK User’s Manual» «N   User-defined functions»
  26. # «hAWK User’s Manual» «P  3   The getline function»
  27. # «hAWK User’s Manual» «O  3   Output into files»
  28. # «hAWK User’s Manual» «Q   The hAWK function»
  29.  
  30. BEGIN { if (spaces_in_tab == "")
  31.             spaces_in_tab = 4
  32.         if (how_many == "")
  33.             how_many = 8
  34.         if (acceptable_length == "")
  35.             acceptable_length = 40
  36.         progressFile = STDPATH "$tempProgress"
  37.     }
  38.  
  39. FNR == 1 {    if (line[1] != "")
  40.                 {
  41.                 if (!progress("sorting..."))
  42.                     { # concurrent mode, print progress to file
  43.                     print "sorting..." > progressFile
  44.                     close(progressFile)
  45.                     }
  46.                 print "line", "\t\t\t", "length (if tabs were converted to spaces)"
  47.                 max = sort(line, ind, "rn")
  48.                 for (i = 1; i <= how_many && i <= max; ++i)
  49.                     {
  50.                     print ind[i], "\t\t\t", line[ind[i]]
  51.                     delete line[ind[i]]
  52.                     }
  53.                 --i;
  54.                 print ""
  55.                 }
  56.             z = split(FILENAME, names, ":")
  57.             ++numFiles
  58.             print names[z], "longest lines are:"
  59.         }
  60.  
  61.     {
  62.     #In a C program, most lines start with tabs but they are
  63.     #less frequent in other positions. Character-by-character
  64.     #analysis is expensive in hAWK, so it’s better to handle
  65.     #special cases using more elegant functions.
  66.     #Still, rather slow, so show what’s happening every 100 lines...
  67.     if (FNR == int(FNR/100)*100)
  68.         {
  69.         if (!progress(names[z] FNR))
  70.             { # concurrent mode, print progress to file
  71.             print names[z], FNR > progressFile
  72.             close(progressFile)
  73.             }
  74.         }
  75.     rawlen = length($0)
  76.     if (match($0,/^\t+/)) #tabs at beginning of line
  77.         {
  78.         position = RLENGTH*spaces_in_tab #account for starting tabs
  79.         temp = substr($0,RLENGTH+1) #trim off the starting tabs
  80.         if (index(temp,"\t") == 0) #no more tabs?
  81.             position += rawlen - RLENGTH
  82.         else #tabs elsewhere besides at start of line
  83.             {
  84.             rawlen -= RLENGTH
  85.             for (i = 1; i <= rawlen; ++i)
  86.                 {
  87.                 char = substr(temp,i,1)
  88.                 if (char == "\t")
  89.                     position = (int(position/spaces_in_tab) + 1) * spaces_in_tab;
  90.                 else
  91.                     ++position;
  92.                 }
  93.             }
  94.         }
  95.     else if (index($0,"\t") == 0) #no tabs in line - not very common
  96.         {
  97.         position = rawlen
  98.         }
  99.     else #tabs somewhere besides beginning of line
  100.         {
  101.         position = 0
  102.         for (i = 1; i <= rawlen; ++i)
  103.             {
  104.             char = substr($0,i,1)
  105.             if (char == "\t")
  106.                 position = (int(position/spaces_in_tab) + 1) * spaces_in_tab;
  107.             else
  108.                 ++position;
  109.             }
  110.         }
  111.     if (position > acceptable_length)
  112.         line[FNR] = position
  113.     }
  114.  
  115. END {if (!progress("sorting..."))
  116.         { # concurrent mode, print progress to file
  117.         print "sorting..." > progressFile
  118.         close(progressFile)
  119.         }
  120.     if (line[1] != "")
  121.         {
  122.         print "line", "\t\t\t", "length (if tabs were converted to spaces)"
  123.         max = sort(line, ind, "rn")
  124.         for (i = 1; i <= how_many && i <= max; ++i)
  125.             {
  126.             print ind[i], "\t\t\t", line[ind[i]]
  127.             }
  128.         --i;
  129.         print ""
  130.         }
  131.     }
  132.